/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CifrarDescifrarSimple.java
 *
 * Created on 23-jun-2010, 14:36:56
 */

package aesphere;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.net.URL;
import javax.help.HelpBroker;
import javax.help.HelpSet;
import javax.swing.JOptionPane;

/**
 *
 * @author antonio
 */
public class CifrarDescifrarSimple extends javax.swing.JFrame {

    private javax.swing.JFrame hijoActual;
    private MainUI wpadre;    

    /** Creates new form CifrarDescifrarSimple */
    public CifrarDescifrarSimple(MainUI padre) {
        initComponents();
        setLangLabels();
        wpadre=padre;
        setHelp();
        this.setResizable(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")


    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Plaintext = new javax.swing.JTextField();
        Ciphertext = new javax.swing.JTextField();
        CiphertextD = new javax.swing.JTextField();
        Key = new javax.swing.JTextField();
        plainText = new javax.swing.JLabel();
        cipherText = new javax.swing.JLabel();
        keyText = new javax.swing.JLabel();
        plainDText = new javax.swing.JLabel();
        KeyD = new javax.swing.JTextField();
        PlaintextD = new javax.swing.JTextField();
        cipherDText = new javax.swing.JLabel();
        keyDText = new javax.swing.JLabel();
        cifrarButton = new javax.swing.JButton();
        descifrarButton = new javax.swing.JButton();
        BotonInfo = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        vector1Pane = new javax.swing.JEditorPane();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane3 = new javax.swing.JScrollPane();
        vector2Pane = new javax.swing.JEditorPane();
        mainMenuBar = new javax.swing.JMenuBar();
        mainMenuArchivo = new javax.swing.JMenu();
        Salir = new javax.swing.JMenuItem();
        mainMenuEditar = new javax.swing.JMenu();
        CopiarCiphertext = new javax.swing.JMenuItem();
        PegarCiphertext = new javax.swing.JMenuItem();
        mainMenuOperaciones = new javax.swing.JMenu();
        CifrarItem = new javax.swing.JMenuItem();
        DescifrarItem = new javax.swing.JMenuItem();
        mainMenuAyuda = new javax.swing.JMenu();
        Contenidos = new javax.swing.JMenuItem();
        acercade = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Comprobar Vectores");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        Plaintext.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                PlaintextKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                PlaintextKeyTyped(evt);
            }
        });

        Ciphertext.setEditable(false);
        Ciphertext.setBorder(null);
        Ciphertext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CiphertextActionPerformed(evt);
            }
        });

        CiphertextD.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                CiphertextDKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                CiphertextDKeyTyped(evt);
            }
        });

        Key.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                KeyActionPerformed(evt);
            }
        });
        Key.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                KeyKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                KeyKeyTyped(evt);
            }
        });

        plainText.setText("Texto en claro:");

        cipherText.setText("Texto cifrado:");

        keyText.setText("Clave:");

        plainDText.setText("Texto en claro:");

        KeyD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                KeyDActionPerformed(evt);
            }
        });
        KeyD.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                KeyDKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                KeyDKeyTyped(evt);
            }
        });

        PlaintextD.setEditable(false);
        PlaintextD.setBorder(javax.swing.BorderFactory.createCompoundBorder());
        PlaintextD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PlaintextDActionPerformed(evt);
            }
        });

        cipherDText.setText("Texto cifrado:");

        keyDText.setText("Clave:");

        cifrarButton.setText("Cifrar");
        cifrarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cifrarButtonActionPerformed(evt);
            }
        });

        descifrarButton.setText("Descifrar");
        descifrarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                descifrarButtonActionPerformed(evt);
            }
        });

        BotonInfo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/info.png"))); // NOI18N
        BotonInfo.setBorder(null);
        BotonInfo.setContentAreaFilled(false);
        BotonInfo.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        BotonInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonInfoActionPerformed(evt);
            }
        });

        vector1Pane.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
        vector1Pane.setContentType("text/html");
        vector1Pane.setEditable(false);
        vector1Pane.setText("<html>\r  \n    <p style=\"margin-top:0px;padding:3px;\">\r\n      En esta herramienta podrá comprobar el correcto funcionamiento de los vectores de prueba del RFC original del AES.<br/>Para ello, algunos se ponen a su disposición <a href=\"\">aquí</a>.\n    </p>\r  \n</html>\r\n");
        vector1Pane.addHyperlinkListener(new javax.swing.event.HyperlinkListener() {
            public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
                vector1PaneHyperlinkUpdate(evt);
            }
        });
        jScrollPane2.setViewportView(vector1Pane);

        vector2Pane.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
        vector2Pane.setContentType("text/html");
        vector2Pane.setEditable(false);
        vector2Pane.setText("<html>\r  \n    <p style=\"margin-top:0px;padding:3px;\">\r\n      Puede probar más vectores descargándose el <a href=\"http://csrc.nist.gov/groups/STM/cavp/documents/aes/KAT_AES.zip\">archivo original</a>.\n    </p>\r  \n</html>\r\n");
        vector2Pane.addHyperlinkListener(new javax.swing.event.HyperlinkListener() {
            public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
                vector2PaneHyperlinkUpdate(evt);
            }
        });
        jScrollPane3.setViewportView(vector2Pane);

        mainMenuArchivo.setText("Archivo");

        Salir.setText("Salir");
        Salir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SalirActionPerformed(evt);
            }
        });
        mainMenuArchivo.add(Salir);

        mainMenuBar.add(mainMenuArchivo);

        mainMenuEditar.setText("Editar");

        CopiarCiphertext.setText("Copiar Ciphertext");
        CopiarCiphertext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CopiarCiphertextActionPerformed(evt);
            }
        });
        mainMenuEditar.add(CopiarCiphertext);

        PegarCiphertext.setText("Pegar Ciphertext");
        PegarCiphertext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PegarCiphertextActionPerformed(evt);
            }
        });
        mainMenuEditar.add(PegarCiphertext);

        mainMenuBar.add(mainMenuEditar);

        mainMenuOperaciones.setText("Operaciones");

        CifrarItem.setText("Cifrar");
        CifrarItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CifrarItemActionPerformed(evt);
            }
        });
        mainMenuOperaciones.add(CifrarItem);

        DescifrarItem.setText("Descifrar");
        DescifrarItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DescifrarItemActionPerformed(evt);
            }
        });
        mainMenuOperaciones.add(DescifrarItem);

        mainMenuBar.add(mainMenuOperaciones);

        mainMenuAyuda.setText("Ayuda");

        Contenidos.setText("Contenidos");
        Contenidos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ContenidosActionPerformed(evt);
            }
        });
        mainMenuAyuda.add(Contenidos);

        acercade.setText("Acerca de");
        mainMenuAyuda.add(acercade);

        mainMenuBar.add(mainMenuAyuda);

        setJMenuBar(mainMenuBar);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 769, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, cifrarButton)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 769, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(plainText)
                            .add(keyText)
                            .add(cipherText))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, Plaintext, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 686, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, Key, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 686, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, Ciphertext, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 686, Short.MAX_VALUE)))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(cipherDText)
                            .add(keyDText)
                            .add(plainDText))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(KeyD, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 686, Short.MAX_VALUE)
                            .add(CiphertextD, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 686, Short.MAX_VALUE)
                            .add(PlaintextD, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 686, Short.MAX_VALUE)))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(163, 163, 163)
                        .add(BotonInfo))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, descifrarButton))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(Plaintext, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(plainText))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(Key, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(keyText))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(Ciphertext, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(cipherText))
                .add(18, 18, 18)
                .add(cifrarButton)
                .add(18, 18, 18)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cipherDText)
                    .add(CiphertextD, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(KeyD, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(keyDText))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(PlaintextD, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(plainDText))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(descifrarButton)
                        .add(34, 34, 34)
                        .add(BotonInfo))
                    .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(44, 44, 44))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void setLangLabels () {
        this.setTitle(Entorno.getTrans("AES.testTitle"));
        mainMenuArchivo.setText(Entorno.getTrans("gen.file"));
        mainMenuEditar.setText(Entorno.getTrans("gen.edit"));
        mainMenuAyuda.setText(Entorno.getTrans("gen.help"));
        Salir.setText(Entorno.getTrans("gen.exit"));
        CopiarCiphertext.setText(Entorno.getTrans("Att.copyCipher"));
        PegarCiphertext.setText(Entorno.getTrans("Att.pasteCipher"));
        Contenidos.setText(Entorno.getTrans("gen.cont"));
        acercade.setText(Entorno.getTrans("gen.about"));
        vector1Pane.setText(Entorno.getTrans("Vec.txt1"));
        vector2Pane.setText(Entorno.getTrans("Vec.txt2"));
        plainText.setText(Entorno.getTrans("AES.plain"));
        plainDText.setText(Entorno.getTrans("AES.plain"));
        cipherText.setText(Entorno.getTrans("AES.cipher"));
        cipherDText.setText(Entorno.getTrans("AES.cipher"));
        keyText.setText(Entorno.getTrans("AES.key") + ":");
        keyDText.setText(Entorno.getTrans("AES.key") + ":");
        cifrarButton.setText(Entorno.getTrans("MainUI.enc"));
        descifrarButton.setText(Entorno.getTrans("MainUI.dec"));
        mainMenuOperaciones.setText(Entorno.getTrans("MainUI.op"));
        CifrarItem.setText(Entorno.getTrans("MainUI.enc"));
        DescifrarItem.setText(Entorno.getTrans("MainUI.dec"));
    }
    
    private void setHelp () {

        Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension ventana = getSize();

        try {
            File fichero = null;

            if (Entorno.getProperty("language").equals("ES"))
                fichero = new File("help/help_set_ES.hs");
            else if (Entorno.getProperty("language").equals("EN"))
                fichero = new File("help/help_set_EN.hs");
            URL hsURL = fichero.toURI().toURL();
            HelpSet helpset = new HelpSet(getClass().getClassLoader(), hsURL);
            HelpBroker hb = helpset.createHelpBroker();
            hb.setLocation(new java.awt.Point((pantalla.width - ventana.width) / 2,
                                (pantalla.height - ventana.height) / 2));
            hb.setSize(new java.awt.Dimension(800, 628));
            hb.enableHelpOnButton(Contenidos, "ventana_vectores", helpset);
            hb.enableHelpOnButton(BotonInfo, "ventana_vectores", helpset);
            hb.enableHelpOnButton(acercade, "aplicacion", helpset);
        }

        catch (Exception e) {
             JOptionPane.showMessageDialog(this, Entorno.getTrans("gen.helpErrMsg"), 
                     Entorno.getTrans("gen.helpErrTitle"), JOptionPane.ERROR_MESSAGE);
        }
    }

    public void wclosed (javax.swing.JFrame hijo){
        if (hijoActual.equals(hijo)) {
            hijoActual.dispose();
            hijoActual = null;
        }
    }



    private boolean ComprobarHexadecimal (String cadena){

    boolean resul = true;

    for (int i = 0; i < cadena.length() ; i++){

      if ( ((cadena.charAt(i) >= 'A') & (cadena.charAt(i) <= 'F')) ||
          ((cadena.charAt(i) >= 'a') & (cadena.charAt(i) <= 'f')) ||
          ((cadena.charAt(i) >= '0') & (cadena.charAt(i) <= '9')));
      else {
          resul=false;
      }
    }

    return resul;


    }


    private boolean ComprobarDatosCifrar () {


        boolean aux=true;

        if ( aux && (Plaintext.getText().isEmpty() || Key.getText().isEmpty()) ){
           JOptionPane.showMessageDialog(this, Entorno.getTrans("AES.fillWarMsg"),
                   Entorno.getTrans("gen.war"), JOptionPane.WARNING_MESSAGE);
           aux=false;
        }



        if ( aux && !ComprobarHexadecimal(Plaintext.getText()) )
             {
                 JOptionPane.showMessageDialog(this, Entorno.getTrans("Att.plainHexWarMsg"),
                   Entorno.getTrans("gen.war"), JOptionPane.WARNING_MESSAGE);
                 aux=false;
             }
        if ( aux && !ComprobarHexadecimal(Key.getText()) )
             {
            JOptionPane.showMessageDialog(this, Entorno.getTrans("AES.hexKeyWarMsg"),
                         Entorno.getTrans("gen.war"), JOptionPane.WARNING_MESSAGE);
            aux=false;
            }

        if ( aux && Plaintext.getText().length()!= 32) {
            JOptionPane.showMessageDialog(this, Entorno.getTrans("Vec.plainLen"),
                         Entorno.getTrans("gen.war"), JOptionPane.WARNING_MESSAGE);
            aux=false;
            }

        if ( aux && (Key.getText().length() != 32 && Key.getText().length() != 48 && Key.getText().length() != 64 )) {
            JOptionPane.showMessageDialog(this, Entorno.getTrans("Vec.keyLen"),
                         Entorno.getTrans("gen.war"), JOptionPane.WARNING_MESSAGE);
            aux=false;
            }

        return aux;
    }


    private boolean ComprobarDatosDescifrar () {


        boolean aux=true;

        if ( aux && (CiphertextD.getText().isEmpty() || KeyD.getText().isEmpty()) ) {
           JOptionPane.showMessageDialog(this, Entorno.getTrans("AES.fillWarMsg"),
                   Entorno.getTrans("gen.war"), JOptionPane.WARNING_MESSAGE);
           aux=false;
        }



        if ( aux && !ComprobarHexadecimal(CiphertextD.getText()) )
             {
                 JOptionPane.showMessageDialog(this, Entorno.getTrans("Att.cipherHexWarMsg"),
                   Entorno.getTrans("gen.war"), JOptionPane.WARNING_MESSAGE);
                 aux=false;
             }
        if ( aux && !ComprobarHexadecimal(KeyD.getText()) ) {
            JOptionPane.showMessageDialog(this, Entorno.getTrans("AES.hexKeyWarMsg"),
                         Entorno.getTrans("gen.war"), JOptionPane.WARNING_MESSAGE);
            aux=false;
        }

        if ( aux && CiphertextD.getText().length()!= 32) {
            JOptionPane.showMessageDialog(this, Entorno.getTrans("Vec.cipherLen"),
                         Entorno.getTrans("gen.war"), JOptionPane.WARNING_MESSAGE);
            aux=false;
        }

        if ( aux && (KeyD.getText().length()!= 32 && KeyD.getText().length()!= 48 && KeyD.getText().length()!= 64 )) {
            JOptionPane.showMessageDialog(this, Entorno.getTrans("Vec.keyLen"),
                         Entorno.getTrans("gen.war"), JOptionPane.WARNING_MESSAGE);
            aux=false;
        }

        return aux;
    }



    private void KeyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_KeyActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_KeyActionPerformed

    private void KeyDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_KeyDActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_KeyDActionPerformed

    private void PlaintextDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PlaintextDActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_PlaintextDActionPerformed

    private void cifrarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cifrarButtonActionPerformed
            CifrarItemActionPerformed(evt);
    }//GEN-LAST:event_cifrarButtonActionPerformed

    private void descifrarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_descifrarButtonActionPerformed
            DescifrarItemActionPerformed(evt);
    }//GEN-LAST:event_descifrarButtonActionPerformed

    private void SalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SalirActionPerformed
        this.dispatchEvent(new java.awt.event.WindowEvent(this, java.awt.event.WindowEvent.WINDOW_CLOSING));
}//GEN-LAST:event_SalirActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        //wpadre.setEnabled(true);
        wpadre.requestFocus();
        wpadre.wclosed(this);
    }//GEN-LAST:event_formWindowClosing

    private void CifrarItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CifrarItemActionPerformed
        if (ComprobarDatosCifrar()) {            
              
              int a=4;

              if (Key.getText().length()==32) a=4;
                else if (Key.getText().length()==48) a=6;
                   else if (Key.getText().length()==64) a=8;

                           
              byte[] in = Conversor.hexStringToByte(Plaintext.getText());             
              byte[] key = Conversor.hexStringToByte(Key.getText());
              AESencrypt aes = new AESencrypt(key, a,false);              
              byte[] out = new byte[16];
              aes.Cipher(in, out);
              Ciphertext.setText(Conversor.byteToHexString(out));
           }
    }//GEN-LAST:event_CifrarItemActionPerformed

    private void DescifrarItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DescifrarItemActionPerformed
      if (ComprobarDatosDescifrar()){

      int a=4;

      if (KeyD.getText().length()==32) a=4;
              else if (KeyD.getText().length()==48) a=6;
                   else if (KeyD.getText().length()==64) a=8;

      byte[] in = Conversor.hexStringToByte(CiphertextD.getText());      
      byte[] key = Conversor.hexStringToByte(KeyD.getText());
      AESdecrypt aesDec = new AESdecrypt(key, a,false);      
      byte[] out = new byte[16];
      aesDec.InvCipher(in, out);
      PlaintextD.setText(Conversor.byteToHexString(out));

      }
    }//GEN-LAST:event_DescifrarItemActionPerformed

    private void CopiarCiphertextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CopiarCiphertextActionPerformed
        Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
        StringSelection ss = new StringSelection(Ciphertext.getText());
        cb.setContents(ss, ss);
}//GEN-LAST:event_CopiarCiphertextActionPerformed

    private void PegarCiphertextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PegarCiphertextActionPerformed
        Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable t = cb.getContents(this);


        try{
            DataFlavor dataFlavorStringJava = new DataFlavor("application/x-java-serialized-object; class=java.lang.String");
            if (t.isDataFlavorSupported(dataFlavorStringJava)) {
                String texto = (String) t.getTransferData(dataFlavorStringJava);                
                CiphertextD.setText(texto);}
        } catch (Exception e){
            JOptionPane.showMessageDialog(this, Entorno.getTrans("gen.pasteErr"),
                    Entorno.getTrans("gen.err"), JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_PegarCiphertextActionPerformed

    private void ContenidosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ContenidosActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_ContenidosActionPerformed

    private void BotonInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonInfoActionPerformed

}//GEN-LAST:event_BotonInfoActionPerformed

    private void vector1PaneHyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {//GEN-FIRST:event_vector1PaneHyperlinkUpdate
        try{
            //si se ha presionado una URL
            if (evt.getEventType() == javax.swing.event.HyperlinkEvent.EventType.ACTIVATED){
                //si no soporta desktop, no se puede abrir el archivo
                if (java.awt.Desktop.isDesktopSupported()) {                    
                    //le quitamos file: a la ruta
                    java.awt.Desktop.getDesktop().open(new File(evt.getURL().toString().substring(5)));
                } else {
                    JOptionPane.showMessageDialog(this, Entorno.getTrans("gen.actNot"),
                        Entorno.getTrans("gen.err"), JOptionPane.ERROR_MESSAGE);
                }
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, Entorno.getTrans("gen.linkErr"),
                Entorno.getTrans("gen.err"), JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_vector1PaneHyperlinkUpdate

    private void CiphertextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CiphertextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_CiphertextActionPerformed

    private void vector2PaneHyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {//GEN-FIRST:event_vector2PaneHyperlinkUpdate
        try{
            //si se ha presionado una URL
            if (evt.getEventType() == javax.swing.event.HyperlinkEvent.EventType.ACTIVATED){
                //si no soporta desktop, no se puede abrir la URL
                if (java.awt.Desktop.isDesktopSupported()) {
                    java.awt.Desktop.getDesktop().browse(evt.getURL().toURI());
                } else {
                    JOptionPane.showMessageDialog(this, Entorno.getTrans("gen.actNot"),
                        Entorno.getTrans("gen.err"), JOptionPane.ERROR_MESSAGE);
                }
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, Entorno.getTrans("gen.linkErr"),
                Entorno.getTrans("gen.err"), JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_vector2PaneHyperlinkUpdate

    private void PlaintextKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_PlaintextKeyTyped
        if (Plaintext.getText().length() > 32) {
            Plaintext.setText(Plaintext.getText().substring(0, 32));
        }
        
        while (!ComprobarHexadecimal(Plaintext.getText())) {
            Plaintext.setText(Plaintext.getText().substring(0,Plaintext.getText().length() - 1));
        }
    }//GEN-LAST:event_PlaintextKeyTyped

    private void PlaintextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_PlaintextKeyReleased
        PlaintextKeyTyped(evt);
    }//GEN-LAST:event_PlaintextKeyReleased

    private void KeyKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_KeyKeyTyped
        if (Key.getText().length() > 64) {
            Key.setText(Key.getText().substring(0, 64));
        }
        
        while (!ComprobarHexadecimal(Key.getText())) {
            Key.setText(Key.getText().substring(0,Key.getText().length() - 1));
        }
    }//GEN-LAST:event_KeyKeyTyped

    private void KeyKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_KeyKeyReleased
        KeyKeyTyped(evt);
    }//GEN-LAST:event_KeyKeyReleased

    private void CiphertextDKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_CiphertextDKeyTyped
        if (CiphertextD.getText().length() > 32) {
            CiphertextD.setText(CiphertextD.getText().substring(0, 32));
        } 
        
        while (!ComprobarHexadecimal(CiphertextD.getText())) {
            CiphertextD.setText(CiphertextD.getText().substring(0,CiphertextD.getText().length() - 1));
        }
    }//GEN-LAST:event_CiphertextDKeyTyped

    private void CiphertextDKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_CiphertextDKeyReleased
        CiphertextDKeyTyped(evt);
    }//GEN-LAST:event_CiphertextDKeyReleased

    private void KeyDKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_KeyDKeyTyped
         if (KeyD.getText().length() > 64) {
            KeyD.setText(KeyD.getText().substring(0, 64));
        }
        while (!ComprobarHexadecimal(KeyD.getText())) {
            KeyD.setText(KeyD.getText().substring(0,KeyD.getText().length() - 1));
        }
    }//GEN-LAST:event_KeyDKeyTyped

    private void KeyDKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_KeyDKeyReleased
        KeyDKeyTyped(evt);
    }//GEN-LAST:event_KeyDKeyReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotonInfo;
    private javax.swing.JMenuItem CifrarItem;
    private javax.swing.JTextField Ciphertext;
    private javax.swing.JTextField CiphertextD;
    private javax.swing.JMenuItem Contenidos;
    private javax.swing.JMenuItem CopiarCiphertext;
    private javax.swing.JMenuItem DescifrarItem;
    private javax.swing.JTextField Key;
    private javax.swing.JTextField KeyD;
    private javax.swing.JMenuItem PegarCiphertext;
    private javax.swing.JTextField Plaintext;
    private javax.swing.JTextField PlaintextD;
    private javax.swing.JMenuItem Salir;
    private javax.swing.JMenuItem acercade;
    private javax.swing.JButton cifrarButton;
    private javax.swing.JLabel cipherDText;
    private javax.swing.JLabel cipherText;
    private javax.swing.JButton descifrarButton;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel keyDText;
    private javax.swing.JLabel keyText;
    private javax.swing.JMenu mainMenuArchivo;
    private javax.swing.JMenu mainMenuAyuda;
    private javax.swing.JMenuBar mainMenuBar;
    private javax.swing.JMenu mainMenuEditar;
    private javax.swing.JMenu mainMenuOperaciones;
    private javax.swing.JLabel plainDText;
    private javax.swing.JLabel plainText;
    private javax.swing.JEditorPane vector1Pane;
    private javax.swing.JEditorPane vector2Pane;
    // End of variables declaration//GEN-END:variables

}
